<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;
use think\Db;

use app\api\controller\Tpmecms;
/**
 * 聊天接口
 */
class Chat extends Tpmecms
{
    protected $noNeedLogin = ['update_unread','update_chat_msg'];
    protected $noNeedRight = ['*'];


    public $uid = '';
    public function _initialize()
    {
        parent::_initialize();
        // header('Access-Control-Allow-Origin: *');
        // $uid = $this->request->param('uid');//用户uid 需要解密
        // if(!empty($uid))$this->auth->id_de($uid);
        // halt($this->auth->id);
        //移除HTML标签
        // $this->request->filter('trim,strip_tags,htmlspecialchars');
    }





    
    /**
     * 获取用户聊天记录
     * @ApiWeigh (999)
     * @param string $re_uid 对方id
     * @param integer $page 默认1
     * @param integer $limit 默认10条
     * @return void
     */
    public function get_user_chat($re_uid,$page=1,$limit=10)
    {
        // if($re_uid>$this->auth->id){
        //     $group = 'im.room.user'.$re_uid.'_'.$this->auth->id;
        // }else{
        //     $group = 'im.room.user'.$this->auth->id.'_'.$re_uid;
        // }

        $group = $this->GetUserGroupName($this->auth->id , $re_uid);
        // $send_uid = $this->auth->id;
        $list = Db::name('chat')
                ->where('group',$group)
                // ->where('re_uid',$re_uid)
                // ->where('send_uid',$send_uid)
                ->order('time desc')
                ->page($page,$limit)
                ->select();

        ////修改消息已读
        foreach ($list as $key => &$val) {
            if($val['msg_status']=='1' && $val['re_uid']==$this->auth->id){
                Db::name('chat')->where('id',$val['id'])->update(['msg_status'=>'2']);//消息状态:1=对方未读,2=已读
            }
            $val['date'] = date('m-d H:i',$val['time']);
        }
        // return json($list);

        if($list){
            $this->success('ok',$list);
        }else{
            $this->error('没有聊天记录',$page);
        }
        
    }

    // //已读消息
    // public function msg_read($id=null)
    // {
    //     return Db::name('chat')->where('re_uid',$this->auth->id)->where('id',$id)->update(['msg_status'=>'2']);
    // }

    /**
     * 获取我的未读消息
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function get_my_msg_list($page=1,$limit=10)
    {
        // $list = Db::name('msg')->where('uid',$this->auth->id)->where('msg_status','1')->order('time desc')->page($page,$limit)->select();
        // foreach ($list as $key => &$val) {
        //     //有点赞的 有聊天的    
        //     switch ($val['status']) {//类型:1=动态点赞,2=动态评论,3=提问点赞,4=提问评论,5=活动点赞,6=活动留言,7=聊天消息
        //         case '7':
        //             $chat = Db::name('chat')->where('id',$val['public_id'])->find();
        //             $val['chat'] = $chat;
        //             $val['unread'] = Db::name('chat')->where('re_uid',$this->auth->id)->where('send_uid',$val['user_id'])->where('msg_status','1')->count();
        //             $val['user'] = $this->GetUserData2($val['user_id']);
        //             break;
                
        //         default:
                    
        //             break;
        //     }
        //     $val['date'] = date('m-d H:i',$val['time']);
        // }

     

        $list = Db::name('msg')->where('uid',$this->auth->id)->where('msg_status','1')->order('time desc')->where('status','7')->page($page,$limit)->select();
        if($list)
        foreach ($list as $key => &$val) {
            $chat = Db::name('chat')->where('id',$val['public_id'])->find();
            $val['chat'] = $chat;
            $val['unread'] = Db::name('chat')->where('re_uid',$this->auth->id)->where('send_uid',$val['user_id'])->where('msg_status','1')->count();
            $val['user'] = $this->GetUserData2($val['user_id']);

            $val['date'] = date('m-d H:i',$val['time']);
        }

        $val2 = Db::name('msg')->where('uid',$this->auth->id)->where('msg_status','1')->order('time desc')->where('status','<>','7')->limit(1)->find();
        if($val2){
            $val2['date'] = date('m-d H:i',$val2['time']);
            $val2['unread'] = Db::name('msg')->where('uid',$this->auth->id)->where('msg_status','1')->order('time desc')->where('status','<>','7')->count();
            $list[] = $val2;
        }
  
        $this->success('ok',$list);
    }

    /**
     * 获取不是聊天消息的消息
     *
     * @param integer $page 1
     * @param integer $limit 100
     * @return void
     */
    public function get_notchat_msg_list($page=1,$limit=100)
    { 
        $list = Db::name('msg')->where('uid',$this->auth->id)->order('time desc')->where('status','<>','7')->page($page,$limit)->select();
        if($list)
        foreach ($list as $key => &$val) {
            $val['date'] = date('m-d H:i',$val['time']);
            $val['user'] = $this->GetUserData2($val['user_id']);
            Db::name('msg')->where('id',$val['id'])->update(['view_status'=>'2']);
        }
        $this->success('ok',$list);
    }



    /**
     * 获取我的未读消息数
     *
     * @return void
     */
    public function get_my_msg_num()
    {
        $list = Db::name('msg')->where('uid',$this->auth->id)->where('msg_status','1')->where('view_status','1')->order('time desc')->select();
        // halt($list);
        $num = 0;
        foreach ($list as $key => &$val) {
            //有点赞的 有聊天的    
            switch ($val['status']) {//类型:1=动态点赞,2=动态评论,3=提问点赞,4=提问评论,5=活动点赞,6=活动留言,7=聊天消息
                // case '1':
                //     // $zone_ids = Db::name('zone')->where('uid',$this->auth->id)->column('id');
                //     // if($zone_ids){
                //     //     Db::name('zonelikes')->where('')
                //     // }
                    
                //     break;

                case '7':
                    // $chat = Db::name('chat')->where('id',$val['public_id'])->find();
                    // $val['chat'] = $chat;
                    $val['unread'] = Db::name('chat')->where('re_uid',$this->auth->id)->where('send_uid',$val['user_id'])->where('msg_status','1')->count();

                    // $val['user'] = $this->GetUserData2($val['user_id']);
                    // if($zone_ids){
                    //     Db::name('zonelikes')->where('')
                    // }
                    // $num = $val['unread'] + $num;
                    break;
                
                default:
                    $num++;
                    break;
            }
            // $val['date'] = date('m-d H:i',$val['time']);
        }
        
        $this->success('ok',$num);
    }




    /**
     * 已读消息
     *
     * @param int $msg_id
     * @return void
     */
    public function msg_read($msg_id)
    {
        $msg = Db::name('msg')->where('id',$msg_id)->find();
        if($msg['status']!='7'){
            $res = Db::name('msg')->where('id',$msg_id)->where('uid',$this->auth->id)->update(['msg_status'=>'2','read_time'=>time()]);
            if($res){
                $this->success('ok');
            }else{
                $this->error('失败');
            }
        }else{
            $this->success('ok');
        }
       
    }


    /**
     * 删除消息
     *
     * @param int $msg_id
     * @return void
     */
    public function del_read($msg_id)
    {
        $res = Db::name('msg')->where('id',$msg_id)->where('uid',$this->auth->id)->find();
        if($res){
            if($res['status']=='7'){
                //修改成已读消息
                $this->update_msg_status($this->auth->id,$res['user_id']);
            }
            Db::name('msg')->where('id',$msg_id)->delete();
            $this->success('删除成功');
        }else{
            $this->error('该记录不存在');
        }
    }

    /**
     * 修改消息状态
     *
     * @param int $re_uid 接收者
     * @param int $send_uid 发送者
     * @return void
     */
    protected function update_msg_status($re_uid,$send_uid)
    {
        Db::name('chat')->where('re_uid',$re_uid)->where('send_uid',$send_uid)->update(['msg_status'=>'2']);//消息状态:1=对方未读,2=已读
    }


    public function update_chat_msg($send_uid=null,$re_uid=null,$title=null,$public_id=null)
    {
        
        $data['status'] = '7';//'类型:1=动态点赞,2=动态评论,3=提问点赞,4=提问评论,5=活动点赞,6=活动留言,7=聊天消息',
        $data['uid'] = $re_uid;//用户ID
        $data['user_id'] = $send_uid;//对方ID


        
        $res = Db::name('msg')->where($data)->find();
        if($res){
            Db::name('msg')->where('id','=',$res['id'])->update(['title'=>$title,'public_id'=>$public_id,'time'=>time()]);
        }else{

            $data['time'] = time();
            $data['public_id'] = $public_id;
            $data['title'] = $title;
            Db::name('msg')->insert($data);
        }



        $data['uid'] = $send_uid;//反一下
        $data['user_id'] = $re_uid;//反一下
        $res = Db::name('msg')->where($data)->find();
        if($res){
            Db::name('msg')->where('id','=',$res['id'])->update(['title'=>$title,'public_id'=>$public_id,'time'=>time()]);
        }else{

            $data['time'] = time();
            $data['public_id'] = $public_id;
            $data['title'] = $title;
            Db::name('msg')->insert($data);
        }

        $this->success('ok');
    }

}
